package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.Awards.Applications;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.system.service.IApplicationsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-03-15
 */
@RestController
@RequestMapping("/system/applications")
public class ApplicationsController extends BaseController
{
    @Autowired
    private IApplicationsService applicationsService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Applications applications)
    {
        startPage();
        List<Applications> list = applicationsService.selectApplicationsList(applications);
        return getDataTable(list);
    }

    @GetMapping("/myList")
    public TableDataInfo myList()
    {   Applications applications=new Applications();
        applications.setStudentId(getUserId());
        startPage();
        List<Applications> list = applicationsService.selectApplicationsList(applications);
        return getDataTable(list);
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Applications applications)
    {
        List<Applications> list = applicationsService.selectApplicationsList(applications);
        ExcelUtil<Applications> util = new ExcelUtil<Applications>(Applications.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{applicationId}")
    public AjaxResult getInfo(@PathVariable("applicationId") Long applicationId)
    {
        return success(applicationsService.selectApplicationsByApplicationId(applicationId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Applications applications)
    {
        applications.setStudentId(getUserId());
        return toAjax(applicationsService.insertApplications(applications));
    }

    /**
     * 学生提交审核
     */
    @PostMapping("/reviewStart")
    public AjaxResult reviewStart(@RequestBody Applications applications)
    {
        applications.setStudentId(getUserId());
        applications.setApplicationStatus("待审核");

        return applicationsService.reviewStart(applications);
    }

    /**
     * 修改【请填写功能名称】
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Applications applications)
    {
        return toAjax(applicationsService.updateApplications(applications));
    }

    /**
     * 删除【请填写功能名称】
     */
    @DeleteMapping("/{applicationIds}")
    public AjaxResult remove(@PathVariable Long[] applicationIds)
    {
        return toAjax(applicationsService.deleteApplicationsByApplicationIds(applicationIds));
    }

    /**
     * 学生根据所在班级选择班级审核人
     */
    @GetMapping("/getClassReviewer")
    public List<Map<String, Object>> getClassReviewer(String roleKey)
    {
       Long deptId= getDeptId();
        return applicationsService.getClassReviewer(deptId,roleKey);
    }

    /**
     * 班主任根据所在学院选择学院审核人
     */
    @GetMapping("/getxueyuanReviewer")
    public List<Map<String, Object>> getxueyuanReviewer(int roleKey)
    {
        if(roleKey==0){
            return  null;
        }
        if(roleKey==2){
            //学校的审核人不需要确定部门
            return applicationsService.getClassReviewer(null,"schoolReviewer");
        }
        Long xueYuanId;
        SysDept dept= getLoginUser().getUser().getDept();
        if(dept==null){
            return  null;
        }
        String[] ancestor= dept.getAncestors().split(",");
        if(ancestor.length==2){
            xueYuanId=dept.getDeptId();
        }else {
            xueYuanId=Long.valueOf(ancestor[2]);
        }
        return applicationsService.getClassReviewer(xueYuanId,"xueyuanReviewer");
    }

    /**
     * 学院的人学校审核人
     */
    @GetMapping("/getSchoolReviewer")
    public List<Map<String, Object>> getSchoolReviewer(String roleKey)
    {

            //学校的审核人不需要确定部门
            return applicationsService.getClassReviewer(null,roleKey);

    }
}
